package 笔记.设计模式.单例模式;

/**
 * @author chenhailong
 * @ClassName: Person
 * @Description: 懒汉
 * @date 2021/6/27 16:31
 * @Version 1.0
 */
public class LazyMan {

    // volatile 防止指令重排
    private static volatile  LazyMan lazyMan = null;

    private LazyMan() {
    }

    // 在需要时再创建，多线程下会出现问题-- 创建多个对象
    public static LazyMan getLazyMan() {
        if (lazyMan == null) {
            lazyMan = new LazyMan();
            return lazyMan;
        }
        return lazyMan;
    }

    // 线程安全版，但是反射不行
    public static LazyMan getLazyMan2() {
        if (lazyMan == null) {
            synchronized (LazyMan.class) {
                if (lazyMan == null) {
                    lazyMan = new LazyMan();
                }
            }
        }
        return lazyMan;
    }




}
